{% extends "data_edit.html" %}
{% load i18n %}

{% block form %}
    {% if request.user|HasPermDefaultGiven:"iaccess.change_accdoor" %}
    {% autoescape off %}
    <tr><td>
        <div class="door_edit_table_title">
            <table id="door_edit_table" cellpadding="3px">
                <tr>
                    {{ form.device|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.door_no|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.door_name|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.lock_active|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.long_open|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.lock_delay|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.card_intervaltime|field_as_td_h }}
                </tr>
                
                <tr id="tr_duration_apb">
                    {{ form.duration_apb|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.door_sensor_status|field_as_td_h }}
                </tr>
                <tr id="tr_sensor_delay">
                    {{ form.sensor_delay|field_as_td_h }}
                </tr>
                <tr id="tr_back_lock">
                    {{ form.back_lock|field_as_td_h }}
                </tr>
                <tr>
                    {{ form.opendoor_type|field_as_td_h }}
                </tr>
                <tr id="tr_card_format">
                    {{ form.wiegand_fmt|field_as_td_h }}
                </tr>
                <tr id="tr_latch_door_type">
                    {{ form.latch_door_type|field_as_td_h }}
                </tr>
                <tr id="tr_latch_time_out">
                    {{ form.latch_time_out|field_as_td_h }}
                </tr>
                <tr id="tr_force_pwd">
                    {{ form.force_pwd|field_as_td_h }}
                </tr>
                <tr id="m_force_pwd">
                    <td align="right"><label id="modify_force_pwd">{%trans "重置胁迫密码"%}:</label></td>
                    <td><input id="pbox1" type="checkbox"></td>
                </tr>
                <tr id="tr_old_force_pwd">
                    <td align="right">{% trans "旧密码"%}:</td>
                    <td><input  type="password" id="old_force_pwd" maxlength="6" onblur="check_force_pwd()"/>&nbsp;<a id="valid_force_pwd" href="javascript:void(0)" onclick="check_force_pwd()">{% trans "验证" %}</a>&nbsp;&nbsp;&nbsp;<span id="r_msg"></span</td>
                </tr>
                <tr id="tr_new_force_pwd">
                    <td align="right">{% trans "新密码"%}:</td>
                    <td><input type="password" id="new_force_pwd"  maxlength="6" onblur="check_force_pass1()" disabled="disabled"></input><span id="e_msg1" class="gray">{% trans "(最大6位整数)" %}</span></td>
                </tr>
                <tr id="tr_conf_force_pwd">
                    <td align="right">{%trans "确认密码"%}:</td>
                    <td><input id="confirm_force_pwd" onblur="check_force_pass2()" type="password"  maxlength="6" disabled="disabled"/><span id="e_msg2"></span></td>
                </tr>
                <tr id="tr_supper_pwd">
                    {{ form.supper_pwd|field_as_td_h }}
                </tr>
                <tr id="m_supper_pwd">
                    <td align="right"><label id="modify_supper_pwd">{%trans "重置紧急状态密码"%}:</label></td>
                    <td><input id="pbox2" type="checkbox"></td>
                </tr>
                <tr id="tr_old_supper_pwd">
                    <td align="right">{% trans "旧密码"%}:</td>
                    <td><input  type="password" id="old_supper_pwd" maxlength="8" onblur="check_supper_pwd()"/>&nbsp;<a id="valid_supper_pwd" href="javascript:void(0)" onclick="check_supper_pwd()">{% trans "验证" %}</a>&nbsp;&nbsp;&nbsp;<span id="r_msg2"></span></td>
                </tr>
                <tr id="tr_new_supper_pwd">
                    <td align="right">{% trans "新密码"%}:</td>
                    <td><input type="password" id="new_supper_pwd"  maxlength="8" onblur="check_supper_pass1()" disabled="disabled"></input><span id="e_msg3" class="gray">{% trans "(8位整数)" %}</span></td>
                </tr>
                <tr id="tr_conf_supper_pwd">
                    <td align="right">{%trans "确认密码"%}:</td>
                    <td><input id="confirm_supper_pwd" onblur="check_supper_pass2()" type="password"  maxlength="8" disabled="disabled"/><span id="e_msg4"></span></td>
                </tr>
                <!-- 
                <tr id="tr_global_apb">
                    {{ form.global_apb|field_as_td_h }}
                </tr>
                 -->
                {% if not "IACCESS_ATTLOG"|filter_config_option %}
                {% else %}
                    <tr>
                        {{ form.is_att|field_as_td_h }}
                    </tr>
                {% endif %}
                <tr>
                    <th><label id="id_lable_settocurall">{% trans "将该设置应用于当前控制器所有门:" %}</label></th>
                    <td>
                        <input type="checkbox" name="applytocur" class="wZBaseBooleanField door_edit_checkbox" id="id_applytocur">
                    </td>
                </tr>

                <tr>
                    <th><label id="id_lable_settoall">{% trans "将该设置应用于所有控制器所有门:" %}</label></th>
                    <td>
                        <input type="checkbox" name="applytoall" class="wZBaseBooleanField door_edit_checkbox" id="id_applytoall">
                    </td>
                </tr>
                <tr>
                    <th>
                        <span style="margin-left:50px"></span>
                        <span>{% trans "出入读头" %}:</span>
                    </th>
                    <td>
                        <span>{% trans "自定义名称" %}</span>
                    </td>
                    {% block reader_extend_header %}
                    {% endblock %}        
                </tr>
                <tr>
                    <th>
                        <div style="float:left;width:68%;">{% trans "读头" %}1</div>
                        <div style="float:left;width:32%;"><span class="gray">{% trans "入" %}</span>:</div>
                    </th>
                    <td>
                        <span>
                            <input type="text" name="reader_in" id="id_reader_in" maxlength="30" />
                            <input type="text" name="reader_id_in" id="id_reader_id_in" class="displayN"/>
                        </span>
                    </td>
                    {% block reader_extend_1 %}
                    {% endblock %}                
                </tr>
                <tr class="displayN">
                    <th>
                        <div style="float:left;width:68%;">{% trans "读头" %}2</div>
                        <div style="float:left;width:32%;"><span class="gray">{% trans "出" %}</span>:</div>
                    </th>
                    <td>
                        <span>
                            <input type="text" id="id_reader_out" name="reader_out" maxlength="30" class="valid"/>
                            <input type="text" id="id_reader_id_out" name="reader_id_out" class="displayN"/>
                        </span>
                    </td>
                    {% block reader_extend_2 %}
                    {% endblock %}
                </tr>
            </table>
        </div>

        {% if form.non_field_errors %}
            <tr><td>{{ form.non_field_errors }}</td></tr>
        {% endif %}
    </td></tr>
    {% endautoescape %}
    {% endif %}<!--change_accdoor-->
{% endblock %}

{% block addjs%}
    {% if request.user|HasPermDefaultGiven:"iaccess.change_accdoor" %}

    var before_submit = function()
    {    
        $("#id_device").attr("disabled",false);
        dev_id=$("#id_device").val()

        if(state1==true&&state2==false)
        {
            if(check_old_force_pwd)
            {
                if(b_check_force_pass1&&b_check_force_pass2)
                {
                    $("#id_force_pwd").attr("value",$("#new_force_pwd").val());
                    return true;
                }else{
                    return false;
                }
            }else
            {
                return false;
            }
        }else if(state1==false&&state2==true)
        {
            if(check_old_supper_pwd)
            {    
                if(b_check_supper_pass1&&b_check_supper_pass2){
                    $("#id_supper_pwd").attr("value",$("#new_supper_pwd").val());
                    return true;
                }else{
                    return false;
                }
            }else
            {
                return false;
            }

        }else if(state1==true&&state2==true)
        {
            if(check_old_force_pwd && check_old_supper_pwd)
            {    
                if(b_check_force_pass1&&b_check_force_pass2&&b_check_supper_pass1&&b_check_supper_pass2){
                    $("#id_force_pwd").attr("value",$("#new_force_pwd").val());
                    $("#id_supper_pwd").attr("value",$("#new_supper_pwd").val());
                    return true;
                }else{
                    return false;
                }
            }        
        }
        return true;
    }
    if($("#id_device").val())//编辑，无新增
    {
        $("#id_device").attr("disabled",true);
        $("#id_door_no").attr("readonly",true);
    }
     if($("#id_force_pwd").val()=="")
     {   
         $("#id_force_pwd").attr("maxlength",8);
     }
     if($("#id_supper_pwd").val()=="")
     {   
         $("#id_supper_pwd").attr("maxlength",8);
     }
    
    $("#tr_force_pwd").hide();
    $("#m_force_pwd").hide();
    $("#tr_old_force_pwd").hide();
    $("#tr_new_force_pwd").hide();
    $("#tr_conf_force_pwd").hide();
    $("#tr_supper_pwd").hide();
    $("#m_supper_pwd").hide();
    $("#tr_old_supper_pwd").hide();
    $("#tr_new_supper_pwd").hide();
    $("#tr_conf_supper_pwd").hide();
    function check_sensor_status(change,val)
    {
        if(val == 0)
        {
            $("#tr_sensor_delay").hide();
            //$("#id_sensor_delay").val('');
            $("#tr_back_lock").hide();
            //$("#id_back_lock").attr("checked",false);
        }
        else
        {
            $("#tr_sensor_delay").show();
            $("#tr_back_lock").show();
            if(change == true)
            {
                $("#id_sensor_delay").val('15');
                $("#id_back_lock").attr("checked",true);
            }
        }
    }
    var change = false;
    var state1 = false;
    var state2 = false;
    if($("#id_force_pwd").val()==""){
        $("#tr_force_pwd").show();
    }else{
        $("#tr_force_pwd").hide();
        $("#m_force_pwd").show();
    }
    $("#modify_force_pwd").click(function(){
        modify_force_pwd();
    });
    $("#pbox1").click(function(){
        modify_force_pwd();
    });
    if($("#id_supper_pwd").val()==""){
        $("#tr_supper_pwd").show();
    }else{
        $("#tr_supper_pwd").hide();
        $("#m_supper_pwd").show();
    }
    $("#modify_supper_pwd").click(function(){
        modify_supper_pwd();
    });
    $("#pbox2").click(function(){
        modify_supper_pwd();
    });
    function modify_force_pwd(){
        if(state1 == false){
            $("#tr_old_force_pwd").show();
            $("#tr_new_force_pwd").show();
            $("#tr_conf_force_pwd").show();
            $("#e_msg1").html('<font color="gray">{%trans "(最大6位整数)"%}</font>');
            $("#pbox1").attr("checked","checked");
            state1 = true;
        }else{
            $("#tr_old_force_pwd").hide();
            $("#tr_new_force_pwd").hide();
            $("#tr_conf_force_pwd").hide();
            $("#pbox1").attr("checked","");
            $("#new_force_pwd").attr("disabled","disabled");
            $("#confirm_force_pwd").attr("disabled","disabled");
            $("#old_force_pwd").attr("value","");
            $("#new_force_pwd").attr("value","");
            $("#confirm_force_pwd").attr("value","");
            $("#r_msg").html("");
            $("#e_msg1").html("");
            $("#e_msg2").html("");
            state1 = false;
        }
    }
    function modify_supper_pwd(){
        if(state2 == false)
        {
            $("#tr_old_supper_pwd").show();
            $("#tr_new_supper_pwd").show();
            $("#tr_conf_supper_pwd").show();
            $("#e_msg3").html('<font color="gray">{%trans "(8位整数)"%}</font>');
            $("#pbox2").attr("checked","checked");
            state2 = true;
        }else
        {
            $("#tr_old_supper_pwd").hide();
            $("#tr_new_supper_pwd").hide();
            $("#tr_conf_supper_pwd").hide();
            $("#pbox2").attr("checked","");
            $("#new_supper_pwd").attr("disabled","disabled");
            $("#confirm_supper_pwd").attr("disabled","disabled");
            $("#old_supper_pwd").attr("value","");
            $("#new_supper_pwd").attr("value","");
            $("#confirm_supper_pwd").attr("value","");
            $("#r_msg2").html("");
            $("#e_msg3").html("");
            $("#e_msg4").html("");

            state2 = false;
        }
    }
    //验证旧的胁迫密码是否正确
    if ($("#id_force_pwd").val() == "")
    {
        $("#id_force_pwd").attr("maxlength", "6");//胁迫密码长度改为6位---huangjs20120628
    }
    var check_old_force_pwd = false;
    function check_force_pwd(){
        var old_force_pwd=$("#old_force_pwd").val();
        var device=$("#id_device").val();
        var door_no=$("#id_door_no").val();
        $.post(
            "/iaccess/check_pwd/",
            {"old_pwd":old_force_pwd,"device":device,"door_no":door_no,"field":"force_pwd"},
            function(xml){
                if(xml == 'ok')
                {
                    $("#r_msg").html("{% trans '正确' %}");
                    check_old_force_pwd = true;
                    $("#new_force_pwd").attr("disabled","");
                    $("#confirm_force_pwd").attr("disabled","");
                }else
                {
                    $("#r_msg").html("<font color='red'>{% trans '错误' %}</font>");
                    check_old_force_pwd = false;
                    $("#new_force_pwd").attr("disabled","disabled");
                    $("#confirm_force_pwd").attr("disabled","disabled");
                }
            }
        );
    }
    //验证旧的超级密码是否正确
    var check_old_supper_pwd = false;
    function check_supper_pwd(){
        var old_supper_pwd=$("#old_supper_pwd").val();
        var device=$("#id_device").val();
        var door_no=$("#id_door_no").val();
        $.post(
            "/iaccess/check_pwd/",
            {"old_pwd":old_supper_pwd,"device":device,"door_no":door_no,"field":"supper_pwd"},
            function(xml){
                if(xml == 'ok'){
                    $("#r_msg2").html("{% trans '正确' %}");
                    check_old_supper_pwd = true;
                    $("#new_supper_pwd").attr("disabled","");
                    $("#confirm_supper_pwd").attr("disabled","");
                }else{
                    $("#r_msg2").html("<font color='red'>{% trans '错误' %}</font>");
                    check_old_supper_pwd = false;
                    $("#new_supper_pwd").attr("disabled","disabled");
                    $("#confirm_supper_pwd").attr("disabled","disabled");
                }
            }
        );
    }
    var b_check_force_pass1=true;
    var b_check_force_pass2=true;
    function check_force_pass1(){
        var v=$("#new_force_pwd").val();
        $("#confirm_force_pwd").val("");
        b_check_force_pass2 = false;
        if(!checkDigit(v)){
            var html="<font color='red'>{%trans "胁迫密码必须为整数"%}</font>";
            $("#e_msg1").html(html);
            b_check_force_pass1=false;
        }else{
            var html='<span id="e_msg1" class="gray">{%trans "(最大6位整数)"%}</span>'
            $("#e_msg1").html(html);
            b_check_force_pass1=true;
        }
    }
    function check_force_pass2(){
        var v1 = $("#new_force_pwd").val();
        var v2 = $("#confirm_force_pwd").val();
        if(v1==v2){
            var html="{% trans '正确' %}";
            $("#e_msg2").html(html);
            b_check_force_pass2=true;
        }else{
            var html="<font color='red'>{%trans '密码必须一致'%}</font>"
            $("#e_msg2").html(html);
            $("#new_force_pwd").attr("value","");
            b_check_force_pass2=false;
        }
    }
    var b_check_supper_pass1=true;
    var b_check_supper_pass2=true;
    function check_supper_pass1(){
        var v=$("#new_supper_pwd").val();
        $("#confirm_supper_pwd").val("");
        b_check_supper_pass2 = false;
        if(!checkDigit(v) || v.length != 8)
        {
            var html="<font color='red'>{%trans "紧急状态密码必须为8位整数"%}</font>";
            $("#e_msg3").html(html);
            b_check_supper_pass1=false;
        }
        else
        {
            var html='<span id="e_msg3" class="gray">{%trans "(8位整数)"%}</span>'
            $("#e_msg3").html(html);
            b_check_supper_pass1=true;
        }
    }
    function check_supper_pass2(){
        var v1 = $("#new_supper_pwd").val();
        var v2 = $("#confirm_supper_pwd").val();
        if(v1==v2)
        {
            if(v1.length != 8 || !checkDigit(v1))
            {
                var html="<font color='red'>{%trans "紧急状态密码必须为8位整数"%}</font>";
                $("#e_msg4").html(html);
                b_check_supper_pass2=false;
            }
            else
            {
                var html="{% trans '正确' %}";
                $("#e_msg4").html(html);
                b_check_supper_pass2=true;
            }
        }
        else{
            var html="<font color='red'>{%trans '密码必须一致'%}</font>"
            $("#e_msg4").html(html);
            $("#new_supper_pwd").attr("value","");
            b_check_supper_pass2=false;
        }
    }
    //验证输入的是否为数字
    function checkDigit(v){
        if(v!=""){
            var temp="0123456789";
            var array = v.split("");
            for(var i=0;i<array.length;i++){
                if(temp.indexOf(array[i])==-1){
                    return false;
                }
            }
        }
        return true;
    }
    function check_sensor_status(change,val)
    {
        if(val == 0)
        {
            $("#tr_sensor_delay").hide();
            //$("#id_sensor_delay").val('');
            $("#tr_back_lock").hide();
            //$("#id_back_lock").attr("checked",false);
        }
        else
        {
            $("#tr_sensor_delay").show();
            $("#tr_back_lock").show();
            if(change == true)
            {
                $("#id_sensor_delay").val('15');
                $("#id_back_lock").attr("checked",true);
            }
        }
    }
    $("#id_edit_form").submit(function(){alert("aa");});

    check_sensor_status(change,$("#id_door_sensor_status").val());
    check_latch_door_type($("#id_latch_door_type").val());

    $("#id_door_sensor_status").change(function(){
        change = true;
        check_sensor_status(change,$(this).val());
        change = false;
    });
    {% block special_js_door_edit %}{% endblock %}<!--供其他app自定义使用-->
    $("#id_latch_door_type").change(function(){
        check_latch_door_type($(this).val());
    });    

    function check_latch_door_type(value)
    {
        if(value == 0)//锁定
        {
            $("#tr_latch_time_out").show();
            if ($("#id_latch_time_out").val() == "")
            {
                $("#id_latch_time_out").val(10);
            }
        }
        else//不锁定
        {
            $("#tr_latch_time_out").hide();
            $("#id_latch_time_out").val("");
        }
    }
    
    $("#id_wiegand_fmt").attr("style","width:170px;");//加大韦根卡的宽度
    
        //获取当前门所属设备的类型-darcy20120329
    $.ajax({
        type: "GET",
        url: "/{{ request.surl }}iaccess/GetData/?func=machine_args&device_id="+ $("#id_device").val(),
        dataType: "json",
        async: false,
        success: function(data)
        {
            //此处根据设备参数决定显示的内容
            var type = data.machine_type;
            var card_format_fun = data.card_format_fun;
            var rexinput_fun = data.rexinput_fun;
            var time_apb_fun = data.time_apb_fun;
            if(type == 12)//一体机
            {
                $(".tr_reader_io_state select").change(function(){
                    
                    var new_value = (parseInt($(this).val())+1)%2;//0变成1，1变成0
                    //alert($(this).val()+'---'+new_value);
                    if($(this).attr("name") == "reader_io_state")//主机
                    {
                        $("#id_reader_io_state_slave").val(new_value);
                    }
                    else
                    {
                        $("#id_reader_io_state").val(new_value);
                    }
                });
            }
            else
            {
                $(".tr_reader_io_state").each(function(){
                    $(this).hide();
                });
                
                if(type==1 || type==2 || type==4 || type==7)//C3控制器不显示跟指纹相关的验证方式。-darcy20120329
                {    
                    $("#id_opendoor_type option").each(function(){
                        var opendoor_type = $(this).val();
                        if(opendoor_type == 1 || opendoor_type == 6 || opendoor_type == 10)//仅指纹，卡加指纹，卡或指纹
                        {
                            $(this).remove();
                        }
                    });
                    if($("#id_opendoor_type").val() == 6)//C3控制器验证方式默认为 仅卡
                    {
                        $("#id_opendoor_type").val(4);
                    }
                }
            }
            if(card_format_fun == 0)
            {
                $("#tr_card_format").hide();
            }
            if(rexinput_fun == 0)
            {
                $("#tr_latch_door_type").hide();
            }
            if(time_apb_fun == 0)
            {
                $("#tr_duration_apb").hide();//暂时直接屏蔽-darcy20120628
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) 
        {
            //alert(textStatus+" "+errorThrown);
            alert(gettext("服务器处理数据失败，请重试！错误码：")+"-628");
        } 
    });
    
    //编辑门读头名称
    var door_id = $("#id_model_pk").val();
    //alert(door_id);
    $.ajax({
        type: "GET",
        url: "/{{ request.surl }}iaccess/GetData/?func=get_reader&door_id=" + door_id,
        dataType: "json",
        async: false,
        success: function(datas)
        {
            $("#id_reader_in").val(datas.reader_name_in);
            $("#id_reader_id_in").val(datas.reader_id_in);
            if(datas.reader_name_out != "")
            {
                $("#id_reader_out").val(datas.reader_name_out);
                $("#id_reader_id_out").val(datas.reader_id_out);
                $("#id_reader_out").parent().parent().parent().removeClass();
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) 
        {
            //alert(textStatus+" "+errorThrown);
            alert(gettext("服务器处理数据失败，请重试！错误码：")+"-628");
        } 
    });
    {% else %}
        alert(gettext("对不起，您没有访问该页面的权限，不能浏览更多信息！"));
        window.location.href = "/{{ request.surl }}accounts/login/";
    {% endif %}<!--add/change_accdoor-->
{% endblock %}

{% block inputfocus %}
    {% if not request.user|HasPermDefaultGiven:"iaccess.change_accdoor" %}
        alert(gettext("对不起，您没有访问该页面的权限，不能浏览更多信息！"));
        window.location.href = "/{{ request.surl }}accounts/login/";
    {% endif %}<!--add/change_accdoor-->
    after_process = function()
    {
        $("#id_edit_form #id_door_name").focus();
    }

{% endblock %}
